<?php

App::uses('AppController', 'Controller');

/**
 * OrdenesTags Controller
 *
 * @property OrdenesTag $OrdenesTag
 */
class ReportesController extends AppController
{

    public $layout = 'epcm';
    public $uses = array(
        'Cambiordene',
        'Ordene',
        'OrdenesTag',
        'DocumentosOrdene',
        'Plano',
        'CambiordenesTag',
        'Contrato',
        'Migra');

    public function index()
    {
        $reportes = $this->Reporte->find('all');
        $this->set(compact('reportes'));
    }

    public function reporte1()
    {

        $sqlCambirdenes = "SELECT ordenes.nombre, co.numero, co.volumen, co.monto, co.original, co.copia, co.original
                            FROM cambiordenes co
                            LEFT JOIN ordenes ON co.ordene_id = ordenes.id
                            WHERE co.volumen = 0
                            OR co.monto = 0";

        $cambioOrdenes = $this->Cambiordene->query($sqlCambirdenes);

        $this->set(compact('cambioOrdenes'));
    }

    public function duplicados()
    {
        $sqlDuplicados = "SELECT COUNT(nombre) as cant , nombre
                            FROM ordenes
                            GROUP BY nombre";
        $ordeneDuplicados = $this->Ordene->query($sqlDuplicados);
        //debug($ordeneDuplicados);
        $this->set(compact('ordeneDuplicados'));
    }

    public function podesigual()
    {
        $sqldes = "SELECT SUM(monto) as total , ordene_id
                    FROM ordenes_tags
                    GROUP BY ordene_id";
        $desiguales = $this->OrdenesTag->query($sqldes);
        $arrayOrdenes = array();
        $c = 0;
        foreach ($desiguales as $d)
        {
            $idOrden = $d['ordenes_tags']['ordene_id'];
            $montoSumaTag = $d['0']['total'];
            $busOrden = $this->Ordene->find('first', array('recursive' => -1, 'conditions' =>
                    array('id' => $idOrden)));
            $montoOrden = $busOrden['Ordene']['monto'];
            if ($montoSumaTag != $montoOrden)
            {
                $arrayOrdenes[$c] = $idOrden;
                $c++;
            }
            //debug($arrayOrdenes);
            //if($b)

        }
        //debug($arrayOrdenes);
        $ordenesDes = $this->Ordene->find('all', array('recursive' => -1, 'conditions' =>
                array('id' => $arrayOrdenes)));

        $this->set(compact('ordenesDes'));
        //debug($desiguales);
        //debug($ordenesDes);
    }

    public function codesigual()
    {
        $sqldes = "SELECT SUM(monto) as total , cambiordene_id
                    FROM cambiordenes_tags
                    GROUP BY cambiordene_id";
        $desiguales = $this->Cambiordene->query($sqldes);
        $arrayCambios = array();
        $c = 0;
        // debug($desiguales);exit;
        foreach ($desiguales as $d)
        {
            $idCambio = $d['cambiordenes_tags']['cambiordene_id'];
            $montoSumaTag = $d['0']['total'];
            $busOrden = $this->Cambiordene->find('first', array('recursive' => -1,
                    'conditions' => array('id' => $idCambio)));
            $montoOrden = $busOrden['Cambiordene']['monto'];
            if ($montoSumaTag != $montoOrden)
            {
                $arrayCambios[$c] = $idCambio;
                $c++;
            }
            //debug($busOrden);
            //if($b)

        }
        // debug($arrayCambios);
        $cambiosDes = $this->Cambiordene->find('all', array('recursive' => 0,
                'conditions' => array('id' => $arrayCambios)));
        debug($cambiosDes);
        $this->set(compact('cambiosDes'));
        //debug($desiguales);
        //debug($ordenesDes);
    }

    public function sinpo()
    {
        $po = $this->Ordene->find('all', array('recursive' => 0, 'conditions' => array('Ordene.monto !=' =>
                    0, )));
        //debug($po);
        $c = 0;
        $cantsinpo = array();
        foreach ($po as $p)
        {
            $idOrden = $p['Ordene']['id'];
            $docpo = $this->DocumentosOrdene->find('first', array('recursive' => -1,
                    'conditions' => array('ordene_id' => $idOrden, 'documento_id' => 19)));
            if (empty($docpo))
            {
                $cantsinpo[$c] = $idOrden;
                $c++;
            }

        }
        //debug($cantsinpo);
        $povol = $this->Ordene->find('all', array('recursive' => -1, 'conditions' =>
                array('id' => $cantsinpo, 'volumenes' => 1)));

        //debug($povol);exit;
        foreach ($povol as $pv)
        {
            $codOrden = $pv['Ordene']['id'];
            $this->request->data['DocumentosOrdene']['ordene_id'] = $codOrden;
            $this->request->data['DocumentosOrdene']['volumen'] = 1;
            $this->request->data['DocumentosOrdene']['documento_id'] = 19;
            $this->request->data['DocumentosOrdene']['original'] = 0;
            $this->DocumentosOrdene->create();
            if ($this->DocumentosOrdene->save($this->request->data))
            {
                echo 'guardo ' . $codOrden;
            }
            //debug($this->request->data);
        }
        //debug($povol);exit;
    }

    public function cotagsnocuadra()
    {
        $cambiOrdene = $this->Cambiordene->find('all', array('recursive' => -1, 'order' =>
                array('ordene_id ASC', 'numero ASC')));

        //debug($cambioOrdene);
        $co = 0;
        $arrayCo = array();
        foreach ($cambiOrdene as $co)
        {
            $idCo = $co['Cambiordene']['id'];
            $nCo = $co['Cambiordene']['numero'];
            $montoCo = $co['Cambiordene']['monto'];

            $sqlSumTag = "SELECT SUM(MONTO) as monto, cambiordene_id FROM cambiordenes_tags WHERE cambiordene_id = $idCo";
            $sumTag = $this->CambiordenesTag->query($sqlSumTag);
            //debug($sumTag); exit;
            if ($nCo == 1)
            {
                $resta = $montoCo - $sumTag;
            }
            //$sumtag =
        }
        //debug($ordenes);
    }

    public function posinpo()
    {
        $po = $this->Ordene->find('all', array('recursive' => 0, 'conditions' => array('Ordene.monto !=' =>
                    0, )));
        //debug($po);
        $c = 0;
        $cantsinpo = array();
        foreach ($po as $p)
        {
            $idOrden = $p['Ordene']['id'];
            $docpo = $this->DocumentosOrdene->find('first', array('recursive' => -1,
                    'conditions' => array('ordene_id' => $idOrden, 'documento_id' => 19)));
            if (empty($docpo))
            {
                $cantsinpo[$c] = $idOrden;
                $c++;
            }

        }
        //debug($cantsinpo);
        $povol = $this->Ordene->find('all', array('recursive' => -1, 'conditions' =>
                array('id' => $cantsinpo, 'volumenes !=' => 1)));
        $this->set(compact('povol'));
        //$this->set(compact('povol'));
        //debug($povol);
    }

    public function regulariza()
    {
        $sqlOrden = "SELECT MIN(transmital) as tran, lugar
                FROM planos
                GROUP BY lugar
                ORDER BY transmital";

        $planos = $this->Plano->query($sqlOrden);
        //debug($planos);
        $c = 1;
        foreach ($planos as $p)
        {
            $lugar = $p['planos']['lugar'];
            $this->Plano->updateAll(array('Plano.nuevolugar' => $c), array('Plano.lugar' =>
                    $lugar));
            $c++;
        }
        $this->redirect(array('controller' => 'Planos', 'action' => 'index'));
    }

    public function generales()
    {
        $this->layout = 'reportes';
        $dcPo = $this->Ordene->find('all', array('recursive' => -1, 'fields' => array('id',
                    'nombre')));

        $sqlProveedor = "SELECT proveedor
                        FROM contratos
                        GROUP BY proveedor";
        $dcProveedor = $this->Contrato->query($sqlProveedor);
        //debug($dcp);

        $this->set(compact('dcPo', 'dcProveedor'));
    }
    public function buscapo()
    {
        //debug($this->request->data);
        //exit;

        $po = $this->request->data['Publicos']['po'];
        $datosPo = $this->Ordene->find('all', array('recursive' => -1, 'conditions' =>
                array('Ordene.nombre LIKE' => '%' . $po . '%')));
        $this->set(compact('datosPo'));
        //debug($datosPo);
    }
    
    public function migra()
    {
        $dMigra = $this->Migra->find('all');
        $contratos = $this->Contrato->find('all', array(
            'recursive'=>-1,        
        ));
        //debug($contratos);
        foreach($contratos as $c)
        {
            $dCon = $this->Migra->find('all', array(
                'recursive'=>-1,
                'conditions'=>array('Migra.codigo'=>$c['Contrato']['codigo'])
            ));
            $idContrato = $c['Contrato']['id'];
            
            $cadena = "";
            foreach($dCon as $d)
            {
                $cad = $d['Migra']['lugar'];
                $cadena .= $cad." ";                
            }
            $data = array('id'=>$idContrato, 'nlugar'=>$cadena);
            $this->Contrato->save($data);
            
            debug($cadena);
        }
    }
}
